package com.cmwa.scbp.sys.dictionary.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cmwa.scbp.base.mvc.controller.BaseController;
import com.cmwa.scbp.base.mvc.entity.Page;
import com.cmwa.scbp.base.mvc.entity.SearchParam;
import com.cmwa.scbp.base.exception.RepositoryException;
import com.cmwa.scbp.base.exception.ValidateFailedException;
import com.cmwa.scbp.sys.dictionary.entity.DictionaryVo;
import com.cmwa.scbp.sys.dictionary.service.DictionaryService;
import com.cmwa.scbp.base.constants.Constant;
import com.cmwa.scbp.utils.cached.DictionaryCached;

@Controller
@RequestMapping("/dictionary")
public class DictionaryController extends BaseController {
	private final static String PAGE_PATH = "system/sys/dictionary/";  //设置jsp的访问路径  by  ex—huangch
	@Resource
	private DictionaryService dictionaryService;
	
	/**
	 * 跳转到数据字典列表页面
	 * @author ex-huangch 2016.5.3
	 */
	@RequestMapping("/dictionaryListView")
	public String dictionaryListView(ModelMap model) {
		return PAGE_PATH+"dictionaryList";
	}
	
	 /**
	  * 数据字典列表分页数据
	  * @author ex-huangch 2016.5.3
	  * @param sp
	  * @return
	  */
	 @RequestMapping("/dictionaryListPage")
	 @ResponseBody
	 public Page<DictionaryVo> dictionaryListPage(SearchParam sp) {
		 Page<DictionaryVo> list = dictionaryService.dictionaryListPage(sp);
		  return list;
	 }	
	
	 /**
	  * 打开新增数据字典页面
	  * @author ex-huangch 2016.5.3
	  * @return
	  */
     @RequestMapping("/dictionaryAddView")
	 public String dictionaryAddView(ModelMap model) {
    	 List<DictionaryVo> dctDat = DictionaryCached.getDictionaryByType(Constant.DCTTYPE_DAT_YES_NOX);
    	 model.addAttribute("dctDat", dctDat);
    	 return PAGE_PATH+"dictionaryAdd";
	 }
     
    /**
 	 * 打开修改系统数据字典页面
 	 * @param dctId
 	 * @param model
 	 * @author ex-huangch
 	 */
 	@RequestMapping("/dictionaryUpdateView")
 	public String dictionaryUpdateView(@RequestParam(value = "dctId") String dctId, ModelMap model) throws ValidateFailedException{
 		List<DictionaryVo> dctDat = DictionaryCached.getDictionaryByType(Constant.DCTTYPE_DAT_YES_NOX);
 		model.addAttribute("dctDat", dctDat);
 		DictionaryVo dictionaryVo = dictionaryService.queryDictionaryByDctId(dctId);
 		model.addAttribute("dictionaryVo", dictionaryVo);
 		return PAGE_PATH+"dictionaryUpdate";
 	}

 	 /**
 	 * 数据库新增系统数据字典
 	 * @param dictionaryVo
 	 * @param model
 	 * @throws RepositoryException
 	 * @author ex-huangch   2016.5.4
 	 */
 	@RequestMapping("/saveDictionary")
 	public String saveDictionary(DictionaryVo dictionaryVo, ModelMap model) throws ValidateFailedException {
 		dictionaryService.saveDictionary(dictionaryVo);
 		return redirectSuccess();
 	}
 	
 	/**
 	 * 删除系统数据字典
 	 * @author ex-huangch 2016.5.4
 	 * @param dctId
 	 * @return
 	 */
 	@RequestMapping("/deleteDictionary")
 	public String deleteDictionary(@RequestParam(value = "dctId") String dctId, ModelMap model) {
 		dictionaryService.deleteDictionary(dctId);
 		return redirectSuccess();
 	}
 	
 	/**
 	 * 更新系统数据字典信息到数据库
 	 * @param dictionaryVo
 	 * @param model
 	 * @return
 	 * @author ex-huangch
 	 */
 	@RequestMapping("/updateDictionary")
 	public String updateDictionary(DictionaryVo dictionaryVo, ModelMap model) throws ValidateFailedException {
 		dictionaryService.updateDictionary(dictionaryVo);
 		return redirectSuccess();
 	   }
 		
 	/**
 	 * 打开查看字典详情信息页面
 	 * @author ex-huangch
 	 * @return
 	 */
 	@RequestMapping("/dictionaryDetailView")
 	public String dictionaryDetailView(@RequestParam(value = "dctId") String dctId, ModelMap model) throws ValidateFailedException {
 		DictionaryVo dictionaryVo = dictionaryService.queryDictionaryByDctId(dctId);
 		model.addAttribute("dictionaryVo", dictionaryVo);
 		return PAGE_PATH+"dictionaryDetail";
 	}
     
}
